package com.js.nickcar.module.addcar.model;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.js.nickcar.R;
import com.js.nickcar.base.BaseHolder;
import com.js.nickcar.entity.remote.CarModelYearBean;
import com.js.nickcar.widget.recycler.BaseRecyclerAdapter;

/**
 * Created by LZY on 2017/10/4.
 */
public class CarModelYearAdapter extends BaseRecyclerAdapter<CarModelYearBean> {

    private CarModelAdapter.onCarModelSelectListener onCarModelSelectListener;

    public CarModelYearAdapter(Context context) {
        super(context, NEITHER);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        CarModelYearHolder holder = new CarModelYearHolder(mInflater.inflate(R.layout.item_car_model_year, parent, false));
        holder.rvCarModel.setLayoutManager(new LinearLayoutManager(mContext));
        CarModelAdapter adapter = new CarModelAdapter(mContext);
        adapter.setOnCarModelSelectListener(onCarModelSelectListener);
        holder.rvCarModel.setAdapter(adapter);
        return holder;
    }

    @Override
    protected void onBindDefaultViewHolder(final RecyclerView.ViewHolder viewHolder, CarModelYearBean item, final int position) {
        CarModelYearHolder holder = (CarModelYearHolder) viewHolder;
        holder.itemView.setSelected(item.isSelected());
        holder.tvModelYear.setText(String.format("%s款", getItem(position).getYear()));
        holder.rvCarModel.setVisibility(item.isSelected() ? View.VISIBLE : View.GONE);
        CarModelAdapter adapter = (CarModelAdapter) holder.rvCarModel.getAdapter();
        adapter.setData(item.getModels());

    }


    public void setOnCarModelSelectListener(CarModelAdapter.onCarModelSelectListener onCarModelSelectListener) {
        this.onCarModelSelectListener = onCarModelSelectListener;
    }

    public static class CarModelYearHolder extends BaseHolder {
        public final TextView tvModelYear;
        public final RecyclerView rvCarModel;

        public CarModelYearHolder(View view) {
            super(view);
            tvModelYear = findViewById(R.id.tv_model_year);
            rvCarModel = findViewById(R.id.rv_car_model);
        }
    }
}
